package com.sike.netty.im.server.handler;

import cn.hutool.json.JSONUtil;
import com.sike.netty.im.common.command.HearBeatRequestCommand;
import com.sike.netty.im.common.command.HearBeatResponseCommand;
import com.sike.netty.im.common.command.Message;
import com.sike.netty.im.common.command.MessageBuilder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 心跳 handler
 *
 * @author chenssy
 * @date 2023/09/25
 */
@Slf4j
public class HearBeatTimerRequestHandler extends SimpleChannelInboundHandler<HearBeatRequestCommand> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HearBeatRequestCommand msg) throws Exception {
        log.info("收到 {} 的心跳数据:{}",ctx.channel(), JSONUtil.toJsonStr(msg));
        Message message = MessageBuilder.buildMessage(new HearBeatResponseCommand("hello"),null,null);
        ctx.writeAndFlush(message);
    }
}
